package com.example.user.web;

import com.example.user.bean.Result;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

// user 端的消息生产者
@RestController
public class MQProduce {

    @Resource
    private RabbitTemplate rabbitTemplate;

    @RequestMapping("fanout")
    public String fanoutMessage(String data){

        for (int i = 0; i < 10; i++) {
            rabbitTemplate.convertAndSend(
                    "fanoutTest",
                    "",
                    new Result(1, "msg" + i, data)
            );
        }
        return "Fanout 消息发送成功: " + data;
    }

    @RequestMapping("direct")
    public String directMessage(String data, String key){
        rabbitTemplate.convertAndSend(
                "directTest",
                key,
                new Result(1, "directMsg", data)
        );
        return "Direct 消息发送成功: " + data;
    }

    @RequestMapping("topic")
    public String topicMessage(String data, String key){
        rabbitTemplate.convertAndSend(
                "topicTest",
                key,
                new Result(1, "topicMsg", data)
        );
        return "topic 消息发送成功: " + data;
    }

}
